﻿Public Class CollegiateCourseList
    Public courseList As List(Of Course)
    Public menuCourse As Menu
    Public radioGroup As StackPanel
    Public assignmentList As CollegiateAssignmentList
    Private currentGuid As String

    Public Sub New()
    End Sub

    Public Sub Sync(ByRef menu As Menu, ByRef radio As StackPanel)
        menuCourse = menu
        radioGroup = radio
    End Sub

    Public Sub Add(ByRef value As Course)
        ' Create course
        courseList.Add(value)

        ' Then register and add to the left options panel
        Dim subj As New RadioButton() With { _
                .Name = String.Concat("rdo", value.guid),
                .Content = value.name,
                .IsChecked = False,
                .GroupName = "OptionsCoursesStack"}

        radioGroup.RegisterName(String.Concat("rdo", value.guid), subj)
        radioGroup.Children.Add(subj)
        AddHandler subj.Checked, AddressOf courseVisibilityChanged

        ' Register and add course to the menu list
        Dim item As New MenuItem() With {
                                .Name = String.Concat("menu", value.guid),
                                .Header = value.name,
                                .IsCheckable = True,
                                .IsChecked = False,
                                .IsEnabled = False}

        menuCourse.RegisterName(String.Concat("menu", value.guid), item)
        menuCourse.Items.Add(item)

        ' Then create a menu item for it, register it, add it to the menu
        Dim itemEdit As New MenuItem() With _
            {.Header = value.name,
             .Name = value.guid}
        AddHandler itemEdit.Click, AddressOf courseMenuRaisedEdit

        Dim itemDelete As New MenuItem() With _
            {.Header = value.name,
             .Name = value.guid}
        AddHandler itemDelete.Click, AddressOf courseMenuRaisedDelete

        Course.courseEditMenu.RegisterName(String.Concat("edit", value.guid), itemEdit)
        Course.courseEditMenu.Items.Add(itemEdit)
        Course.courseDeleteMenu.RegisterName(String.Concat("delete", value.guid), itemDelete)
        Course.courseDeleteMenu.Items.Add(itemDelete)
    End Sub

    Public Sub courseMenuRaisedEdit(ByVal sender As MenuItem, ByVal e As System.Windows.RoutedEventArgs)
        currentGuid = sender.Name
        Dim value As Course = courseList.Find(AddressOf SameCourse)
        Edit(value)
    End Sub

    Public Sub courseMenuRaisedDelete(ByVal sender As MenuItem, ByVal e As System.Windows.RoutedEventArgs)
        If courseList.Count > 0 Then
            currentGuid = sender.Name
            Dim value As Course = courseList.Find(AddressOf SameCourse)
            Delete(value)
        Else
            MessageBox.Show("Sorry! You cannot delete the last course!" & _
                            vbNewLine & "You must have at least one active course.")
        End If
    End Sub

    Public Sub Delete(ByRef value As Course)
            ' Delete each assignment
            While value.assignments.Count > 0
                assignmentList.assignmentRaisedDelete(value.assignments(0).guid)
            End While

            ' Then delete radio button and view menu occurances
            Dim subj As RadioButton = radioGroup.FindName(String.Concat("rdo", value.guid))
            radioGroup.Children.Remove(subj)

            Dim item As MenuItem = menuCourse.FindName(String.Concat("menu", value.guid))
            menuCourse.Items.Remove(item)

            ' Then delete the course from both edit and delete menus
            Dim itemEdit As MenuItem = Course.courseEditMenu.FindName(String.Concat("edit", value.guid))
            Course.courseEditMenu.Items.Remove(itemEdit)

            Dim itemDelete As MenuItem = Course.courseDeleteMenu.FindName(String.Concat("delete", value.guid))
            Course.courseDeleteMenu.Items.Remove(itemDelete)

            value.Delete()
    End Sub

    Public Sub Edit(ByRef value As Course)
        value.Edit()
        update(value)
    End Sub

    Public Sub update(ByRef value As Course)
        Dim subj As RadioButton = radioGroup.FindName(String.Concat("rdo", value.guid))
        subj.Content = value.name

        Dim item As MenuItem = menuCourse.FindName(String.Concat("menu", value.guid))
        item.Header = value.name

        Dim itemEdit As MenuItem = Course.courseEditMenu.FindName(String.Concat("edit", value.guid))
        itemEdit.Header = value.name

        Dim itemDelete As MenuItem = Course.courseDeleteMenu.FindName(String.Concat("delete", value.guid))
        itemDelete.Header = value.name

        For Each task As Assignment In value.assignments
            assignmentList.Update(task)
        Next
    End Sub

    Public Sub courseVisibilityChanged(ByVal sender As RadioButton, ByVal e As System.Windows.RoutedEventArgs)
            assignmentList.courseVisibility = sender.Content
    End Sub

    Private Function SameCourse(ByVal value As Course) As Boolean
        If value.guid = currentGuid Then
            Return True
        Else
            Return False
        End If
    End Function

End Class
